package demo1;

import demo2.*;

public class Tetromino {
    public Cell[] cells = new Cell[4];
    public State[] states;
    public int count = 0;

    // 顺时针旋转方法
    public void rotateRight(){
        if(states == null || states.length == 0){
            return;
        }
        count = (count + 1) % states.length;
        Spin();
    }

    // 逆时针旋转方法
    public void rotateLeft(){
        if(states == null || states.length == 0){
            return;
        }
        count = (count - 1 + states.length) % states.length;
        Spin();
    }

    private void Spin() {
        State s = states[count];
        Cell cell = cells[0];
        int row = cell.getRow();
        int col = cell.getCol();
        cells[1].setRow(row + s.row1);
        cells[1].setCol(col + s.col1);
        cells[2].setRow(row + s.row2);
        cells[2].setCol(col + s.col2);
        cells[3].setRow(row + s.row3);
        cells[3].setCol(col + s.col3);
    }

    // 编写四方格旋转状态的内部类
    protected static class State {
        int row0, col0, row1, col1, row2, col2, row3, col3;

        public State(int row0, int col0, int row1, int col1, int row2, int col2, int row3, int col3) {
            this.row0 = row0;
            this.col0 = col0;
            this.row1 = row1;
            this.col1 = col1;
            this.row2 = row2;
            this.col2 = col2;
            this.row3 = row3;
            this.col3 = col3;
        }

        @Override
        public String toString() {
            return "State{" +
                    "row0=" + row0 +
                    ", col0=" + col0 +
                    ", row1=" + row1 +
                    ", col1=" + col1 +
                    ", row2=" + row2 +
                    ", col2=" + col2 +
                    ", row3=" + row3 +
                    ", col3=" + col3 +
                    '}';
        }
    }

    // 左移方法
    public void moveLeft(){
        for (Cell cell : cells) {
            cell.left();
        }
    }

    // 右移方法
    public void moveRight(){
        for (Cell cell : cells) {
            cell.right();
        }
    }

    // 下落方法
    public void softDrop(){
        for (Cell cell : cells) {
            cell.drop();
        }
    }

    // 随机生成四方格的方法
    public static Tetromino randomOne(){
        int num = (int)(Math.random() * 7);
        Tetromino tetromino = null;
        switch (num){
            case 0:
                tetromino = new I();
                break;
            case 1:
                tetromino = new J();
                break;
            case 2:
                tetromino = new L();
                break;
            case 3:
                tetromino = new O();
                break;
            case 4:
                tetromino = new S();
                break;
            case 5:
                tetromino = new T();
                break;
            case 6:
                tetromino = new Z();
                break;
            default:
        }
        return tetromino;
    }
}
